package com.photoeditor.demo.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.photoeditor.R;

public class FingerTouchButton extends FrameLayout {

    LayoutInflater mRootView;

    RippleDetector mRipple;

    TextView mTv;

    ImageView mIvFinger;

    Animator f13159e;
    private Path f;
    private RectF g;
    private int h;

    public FingerTouchButton(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FingerTouchButton(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        a(attributeSet);
    }

    private void a(AttributeSet attributeSet) {
        setClickable(true);
        setWillNotDraw(false);
        if (getBackground() == null) {
            setBackgroundResource(R.drawable.bg_subscribe_btn);
        }
        this.mRootView = LayoutInflater.from(getContext());
        View inflate = this.mRootView.inflate(R.layout.subscribe_button, this, true);
        this.mTv = (TextView) inflate.findViewById(R.id.pay_btn);
        this.mIvFinger = (ImageView) inflate.findViewById(R.id.arrow_iv);
        this.mRipple = new RippleDetector(getContext(), this, null, -1);
        this.h = a(getContext(), 28.0f);
        this.mRipple.a(800);
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, R.styleable.FingerTouchButton, 0, 0);
        if (obtainStyledAttributes.hasValue(0)) {
            this.mTv.setText(obtainStyledAttributes.getString(R.styleable.FingerTouchButton_text_string));
        }
        obtainStyledAttributes.recycle();
    }

    public int a(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public void setButtonText(String str) {
        this.mTv.setText(str);
    }

    public void setButtonTextSize(float f2) {
        this.mTv.setTextSize(1, f2);
    }

    @Override
    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        this.mRipple.a(i, i2);
        this.g = new RectF(0.0f, 0.0f, (float) i, (float) i2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        return this.mRipple.a(motionEvent, super.onTouchEvent(motionEvent));
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.h > 0) {
            if (this.f == null) {
                this.f = new Path();
            }
            if (this.g == null) {
                this.g = new RectF(0.0f, 0.0f, (float) getWidth(), (float) getHeight());
            }
            this.f.reset();
            Path path = this.f;
            RectF rectF = this.g;
            int i = this.h;
            path.addRoundRect(rectF, (float) i, (float) i, Direction.CCW);
            canvas.clipPath(this.f);
        }
        this.mRipple.a(canvas);
    }

    @Override
    public void onDetachedFromWindow() {
        this.mRipple.d();
        this.mRipple.e();
        Animator animator = this.f13159e;
        if (animator != null) {
            animator.cancel();
        }
        super.onDetachedFromWindow();
    }

    public RippleDetector getRippleDetector() {
        return this.mRipple;
    }

    public void a() {
        this.mRipple.a(true);
        this.mRipple.c();
        if (this.f13159e == null) {
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator ofFloat = ObjectAnimator.ofFloat(this.mIvFinger, "scaleX", new float[]{1.0f, 0.8f, 1.0f});
            ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(this.mIvFinger, "scaleY", new float[]{1.0f, 0.8f, 1.0f});
            ofFloat.setDuration(800);
            ofFloat.setRepeatMode(ValueAnimator.REVERSE);
            ofFloat.setRepeatCount(-1);
            ofFloat2.setDuration(800);
            ofFloat2.setRepeatMode(ValueAnimator.REVERSE);
            ofFloat2.setRepeatCount(-1);
            animatorSet.playTogether(new Animator[]{ofFloat, ofFloat2});
            this.f13159e = animatorSet;
        }
        this.f13159e.start();
        this.mIvFinger.setVisibility(View.VISIBLE);
    }

    public void cancel() {
        c();
        this.mIvFinger.setVisibility(View.INVISIBLE);
    }

    public void c() {
        this.mRipple.a(false);
        this.mRipple.d();
        Animator animator = this.f13159e;
        if (animator != null) {
            animator.cancel();
        }
    }

    public TextView getTv() {
        return mTv;
    }
}
